/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.h5Area.service;

import com.fzhucc.modules.h5Area.domain.H5Area;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.h5Area.repository.H5AreaRepository;
import com.fzhucc.modules.h5Area.domain.dto.H5AreaDto;
import com.fzhucc.modules.h5Area.domain.dto.H5AreaQueryCriteria;
import com.fzhucc.modules.h5Area.service.mapstruct.H5AreaMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-02-07
**/
@Service
@RequiredArgsConstructor
public class H5AreaService {

    private final H5AreaRepository h5AreaRepository;
    private final H5AreaMapper h5AreaMapper;

    public Map<String,Object> queryAll(H5AreaQueryCriteria criteria, Pageable pageable){
        Page<H5Area> page = h5AreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(h5AreaMapper::toDto));
    }

    public Map<String,Object> queryAllh5(H5AreaQueryCriteria criteria, Pageable pageable){
        Page<H5Area> page = h5AreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(h5AreaMapper::toDto));
    }

    public List<H5AreaDto> queryAll(H5AreaQueryCriteria criteria){
        return h5AreaMapper.toDto(h5AreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public H5AreaDto findById(Integer id) {
        H5Area h5Area = h5AreaRepository.findById(id).orElseGet(H5Area::new);
        ValidationUtil.isNull(h5Area.getId(),"H5Area","id",id);
        return h5AreaMapper.toDto(h5Area);
    }

    @Transactional(rollbackFor = Exception.class)
    public H5AreaDto create(H5Area resources) {
        return h5AreaMapper.toDto(h5AreaRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(H5Area resources) {
        H5Area h5Area = h5AreaRepository.findById(resources.getId()).orElseGet(H5Area::new);
        ValidationUtil.isNull( h5Area.getId(),"H5Area","id",resources.getId());
        h5Area.copy(resources);
        h5AreaRepository.save(h5Area);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            h5AreaRepository.deleteById(id);
        }
    }

    public void download(List<H5AreaDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (H5AreaDto h5Area : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", h5Area.getName());
            map.put("图片地址", h5Area.getPic());
            map.put("值  地区传的值", h5Area.getVal());
            map.put("排序", h5Area.getSort());
            map.put("h5是否展示  默认不展示", h5Area.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}